﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DialogueManager : MonoBehaviour {

	public DialogueManagerData dm;
	public UIDisplay ui;
	public Settings set;

	public bool isShowing;

	public bool isPressed;
	bool onHidePressed;
	bool onShowPressed;
	bool onSkipPressed;

	void Awake()
	{
		dm = FindObjectOfType<DialogueManagerData> ();
		ui = FindObjectOfType<UIDisplay> ();
		set = FindObjectOfType<Settings> ();
	}
	public void StartDialogue(DialoguerDialogues diaDia)
	{
		dm.setTextLabel (GameObject.Find ("text"));
		dm.setNameLabel (GameObject.Find ("nametext"));

		Dialoguer.Initialize ();
		Dialoguer.events.onStarted += onStart;
		Dialoguer.events.onEnded += onEnd;
		Dialoguer.events.onTextPhase += onTextPhase;

		dm.setDialogueID(diaDia);
		Dialoguer.StartDialogue (dm.getDialogueID (), DialoguerCallback);
		//print (diaDia);
		this.enabled = false;

		dm.setSkip (false);
		dm.setAuto (false);
		dm.setHide (false);
	}

	public void EndDialogue()
	{
		Dialoguer.EndDialogue ();
	}


	public void UserInput()
	{
		OnHide ();//--hide text functionality
		if(!isPressed && Input.GetKeyDown(KeyCode.Space))
		{
			dm.setAuto(false);
			dm.setSkip(false);

			if(dm.getTextLabel().GetComponent<TypewriterEffect>() == null && dm.getChoices().Length == 1)
			{
				Dialoguer.ContinueDialogue();
			}
			else
			{
				Destroy(dm.getTextLabel().GetComponent<TypewriterEffect>());
				dm.getTextLabel().GetComponent<UILabel>().text = dm.getDialogue();
				if(GameObject.FindGameObjectWithTag("Actor") != null)
					GameObject.FindGameObjectWithTag("Actor").GetComponent<AnimationManager>().ClearStack();
			}
		}
		isPressed = Input.GetKey (KeyCode.Space);

		if(!onHidePressed && Input.GetKey(KeyCode.H))
		{
			dm.setHide(!dm.getHide());
		}
		onHidePressed = Input.GetKey (KeyCode.H);

		if(!onSkipPressed && Input.GetKey(KeyCode.S))
		{
			skip ();
		}
	}
	//NOTE: This is temporary.
	public void skip()
	{
		if(dm.getSkip() == true && dm.getChoices().Length == 1)
		{
			Dialoguer.ContinueDialogue();
			if(dm.getTextLabel().GetComponent<TypewriterEffect>() != null)
			{
				Destroy (dm.getTextLabel().GetComponent<TypewriterEffect>());
			}
			dm.getTextLabel().SetActive(true);
		}


	}

	public void OnSkip()
	{
		if(dm.getSkip() == true && set.toSkipUnreadText == true && dm.getChoices().Length == 1)
		{
			Dialoguer.ContinueDialogue();
			if(dm.getTextLabel().GetComponent<TypewriterEffect>() != null)
			{
				Destroy (dm.getTextLabel().GetComponent<TypewriterEffect>());
			}
			dm.getTextLabel().SetActive(true);
		}
	}

	public void OnAuto()
	{
		if(dm.getAuto() == true && dm.getChoices().Length == 1)
		{
			if(dm.getTextLabel().GetComponent<TypewriterEffect>() == null)
			{
				StartCoroutine (autoDialogue());
			}
		}
	}

	IEnumerator autoDialogue()
	{
		//dm.getAuto () = false;
		dm.setAuto (false);
		yield return new WaitForSeconds (set.AutoPrintSpeed);
		Dialoguer.ContinueDialogue ();
		dm.setAuto (true);
		//dm.getAuto () = true;
	}

	public void OnHide()
	{
		if(dm.getHide() == true)
		{
			for(int i=0; i<ui.ingameUI.Count; i++)
			{
				ui.ingameUI[i].SetActive(false);
			}
		}

		else
		for(int i=0; i<ui.ingameUI.Count; i++)
		{
			ui.ingameUI[i].SetActive(true);
		}
	}
	 
	public void OnChoiceSelect(GameObject sender)
	{
		Debug.Log (sender.name);
		Dialoguer.ContinueDialogue (int.Parse(sender.name));
	}

	private void onStart()
	{
		isShowing = true;
	}

	private void onEnd()
	{
		isShowing = false;

	}

	private void onTextPhase(DialoguerTextData data)
	{
		dm.setDialogue (data.text);
		dm.setName (data.name);

		if (data.choices.Length != null)
			dm.setChoices (data.choices);
		else
			dm.setChoices (null);

		if(dm.getTextLabel() == null)
		dm.setTextLabel (GameObject.Find ("text"));
		
		if (dm.getNameLabel () == null)
		dm.setNameLabel (GameObject.Find ("nametext"));

		dm.getTextLabel ().GetComponent<UILabel> ().text = dm.getDialogue ();
		dm.getNameLabel ().GetComponent<UILabel> ().text = dm.getName ();

		dm.getTextLabel ().AddComponent ("TypewriterEffect");
		dm.getTextLabel().GetComponent<TypewriterEffect>().charsPerSecond = (int)GameObject.Find("MenuManager").GetComponent<Settings>().textSpeed;
	}

	private void DialoguerCallback()
	{
		this.enabled = true;
	}

	void OnLevelWasLoaded(int level)
	{
		dm.setSkip(false);
		dm.setAuto(false);
	}

}
